// Copyright 叶湘 ( weixin:yexiang841, email:yexiang841@qq.com ), Shitouren Studio. All Rights Reserved.

#include "Character/Q3D_CPPA_Character_Alpha.h"
#include "Property/Q3D_CPPC_PropertyState.h"
#include "Property/Q3D_CPPE_PropertyName.h"
#include "Hit/Q3D_CPPC_HitTarget.h"
#include "Engine/DataTable.h"
#include "Logger/Q3D_CPPU_Logger.h"

// Sets default values
AQ3D_CPPA_Character_Alpha::AQ3D_CPPA_Character_Alpha()
{
	this->Ptr_C_HitTarget = CreateDefaultSubobject<UQ3D_CPPC_HitTarget>(TEXT("UQ3D_CPPC_HitTarget"));
}

// Called when the game starts or when spawned
void AQ3D_CPPA_Character_Alpha::BeginPlay()
{
	Super::BeginPlay();

	// 基类的 BeginPlay() 通过调用本类的 Q3D_IF_PropertyState_InitDT_Get() 向 PropertyState 中写入了初始数据

	// 绑定一些属性关联
	this->Ptr_C_PropertyState->Q3D_F_Property_Bind_Max_To_Now(EQ3D_CPPE_PropertyName::LIVE_HP_MAX, EQ3D_CPPE_PropertyName::LIVE_HP_NOW);
	this->Ptr_C_PropertyState->Q3D_F_Property_Bind_Max_To_Now(EQ3D_CPPE_PropertyName::MANA_MP_MAX, EQ3D_CPPE_PropertyName::MANA_MP_NOW);
	this->Ptr_C_PropertyState->Q3D_F_Property_Bind_Max_To_Now(EQ3D_CPPE_PropertyName::ENERGY_STA_MAX, EQ3D_CPPE_PropertyName::ENERGY_STA_NOW);

	// 新增一些自然回复
	float float_hit_interval = 0.5f;
	UQ3D_CPPU_Hit *ptr_u_hit;
	this->Ptr_C_PropertyState->Q3D_F_Create_Hit_On_Now_By_Spd(EQ3D_CPPE_PropertyName::MANA_MP_SPD, EQ3D_CPPE_PropertyName::MANA_MP_NOW, float_hit_interval, ptr_u_hit);
	this->Ptr_C_PropertyState->Q3D_F_Create_Hit_On_Now_By_Spd(EQ3D_CPPE_PropertyName::LIVE_HP_SPD, EQ3D_CPPE_PropertyName::LIVE_HP_NOW, float_hit_interval, ptr_u_hit);
	this->Ptr_C_PropertyState->Q3D_F_Create_Hit_On_Now_By_Spd(EQ3D_CPPE_PropertyName::ENERGY_STA_SPD, EQ3D_CPPE_PropertyName::ENERGY_STA_NOW, float_hit_interval, ptr_u_hit);
}

void AQ3D_CPPA_Character_Alpha::Q3D_IF_PropertyState_InitDT_Get_Implementation(UDataTable *&ptr_dt_property_init) const
{
	// Owner 已经从外部对 Ptr_DT_PropertyInit 进行了赋值，优先级最高，直接使用
	if (this->Ptr_DT_PropertyInit)
	{
		ptr_dt_property_init = this->Ptr_DT_PropertyInit;
		Q3D_LOG(Log, "DT_Load_From_Ptr_DT_PropertyInit");
		return;
	}

	// 子类未赋值DT，就用默认的
	const FString string_dt_path_tmp = "DataTable'/Q3D_Lib/Q3D/Property/Q3D_DT_Property_Character_Alpha.Q3D_DT_Property_Character_Alpha'";

	// 结构体
	// EQ3D_CPPE_PropertyName Enum_PropertyName;
	// bool Bool_Active;
	// int32 Int_Min;
	// int32 Int_Max;
	// EQ3D_CPPE_PropertyInit Enum_PropertyInit;
	// int32 Int_Value;

	UDataTable *ptr_dt_property_init_tmp;

	// DT的一种读法
	// static ConstructorHelpers::FObjectFinder<UDataTable> finder_tmp( string_dt_path_tmp );
	// if( finder_tmp.Object ){ ptr_dt_property_init_tmp = ( UDataTable* )( finder_tmp.Object ); }

	// DT的另一种读法
	ptr_dt_property_init_tmp = LoadObject<UDataTable>(NULL, UTF8_TO_TCHAR(*string_dt_path_tmp));

	if (!ptr_dt_property_init_tmp)
	{
		Q3D_LOG(Warning, "DT_Missed_From_[" + string_dt_path_tmp + "]");
		ptr_dt_property_init = nullptr;
		return;
	}
	ptr_dt_property_init = ptr_dt_property_init_tmp;
	Q3D_LOG(Log, "DT_Load_From_[" + string_dt_path_tmp + "]");
	return;
}

void AQ3D_CPPA_Character_Alpha::Q3D_IF_Hit_Set_Implementation(UQ3D_CPPU_Hit *ptr_u_hit, bool &bool_success, FString &string_log)
{
	this->Ptr_C_HitTarget->Q3D_F_Hit_Set(ptr_u_hit, bool_success, string_log);
	return;
}

void AQ3D_CPPA_Character_Alpha::Q3D_IF_Hit_Cancel_ById_Implementation(FName name_id, bool &bool_success, FString &string_log)
{
	this->Ptr_C_HitTarget->Q3D_F_Hit_Cancel_ById(name_id, bool_success, string_log);
	return;
}

void AQ3D_CPPA_Character_Alpha::Q3D_IF_Hit_Cancel_ByType_Implementation(EQ3D_CPPE_HitType enum_hit_type, int32 &int_count)
{
	this->Ptr_C_HitTarget->Q3D_F_Hit_Cancel_ByType(enum_hit_type, int_count);
	return;
}

void AQ3D_CPPA_Character_Alpha::Q3D_IF_Hit_Cancel_ByCauser_Implementation(EQ3D_CPPE_HitType enum_hit_type, AActor *ptr_a_hit_causer, int32 &int_count)
{
	this->Ptr_C_HitTarget->Q3D_F_Hit_Cancel_ByCauser(enum_hit_type, ptr_a_hit_causer, int_count);
	return;
}

void AQ3D_CPPA_Character_Alpha::Q3D_IF_Hit_Cancel_All_Implementation(int32 &int_count)
{
	this->Ptr_C_HitTarget->Q3D_F_Hit_Cancel_All(int_count);
	return;
}
